<template>
  <div>
    <template class="c-field" v-if="display">{{ display }}</template>
    <template v-else>
      <span class="c-field-placeholder">{{ placeholder }}</span>
    </template>
    <van-popup
      v-model="visible"
      position="bottom"
      get-container="body"
      class="c-yd54lx"
    >
      <div class="flex-row flex-align-center flex-justify-between">
        <div class="c-yd54lx-cancel" @click="visible = false">取消</div>
        <div class="c-yd54lx-title">{{ title }}</div>
        <div class="c-yd54lx-confirm" @click="handleClick">确认</div>
      </div>

      <van-datetime-picker
        v-model="currentDate"
        type="date"
        :title="title"
        :show-toolbar="false"
        :columns-order="['year', 'month', 'day']"
        :formatter="formatter"
        :min-date="minDate"
        :max-date="maxDate"
      />
    </van-popup>
  </div>
</template>
<script>
export default {
  props: {
    title: {
      type: String,
      default: ''
    },
    placeholder: {
      type: String,
      default: ''
    },
    defaultValue: {
      type: String,
      default: ''
    },
    columns: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    defaultValue(val) {
      this.value = val
    }
  },
  computed: {
    display() {
      return this.value
    }
  },
  data() {
    return {
      visible: false,
      value: '',
      currentDate: new Date(),
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(new Date().getFullYear() + 100, 10, 1)
    }
  },
  created() {
    this.init()
  },
  methods: {
    async init() {
      if (this.defaultValue) {
        this.currentDate = new Date(this.defaultValue)
        this.value = JSON.parse(
          JSON.stringify(this.getCurrentDate(this.currentDate))
        )
      }
    },
    formatter(type, val) {
      if (type === 'year') {
        return val
      }
      if (type === 'month') {
        return val
      }
      if (type === 'day') {
        return val
      }
      return val
    },
    handleClick() {
      this.$emit('confirm', this.getCurrentDate(this.currentDate))
      this.value = JSON.parse(
        JSON.stringify(this.getCurrentDate(this.currentDate))
      )

      this.visible = false
    },
    handleChange(val) {
      console.log('dddd============time===val', val)
      //   let data = val.getValues().join('-')
      //   this.currentDate = new Date(data)
    },
    getCurrentDate(time) {
      let year = time.getFullYear()
      let month =
        time.getMonth() + 1 > 9
          ? time.getMonth() + 1
          : '0' + (time.getMonth() + 1)
      let day = time.getDate() > 9 ? time.getDate() : '0' + time.getDate()
      return year + '-' + month + '-' + day
    }
  }
}
</script>
<style lang="less" scoped>
.c-field {
  font-size: 15px;
  color: #333;
}
.c-field-placeholder {
  color: #999;
  font-size: 15px;
}
.c-yd54lx {
  max-height: 500px;
  border-radius: 13px 13px 0 0;
  text-align: center;
  padding: 15px;
  box-sizing: border-box;
  &-cancel,
  &-confirm {
    font-size: 14px;
    color: @c-color-primary;
  }
  &-title {
    font-size: 18px;
    color: #333;
    font-weight: bold;
  }
  &-item {
    font-size: 15px;
    color: #666;
    margin-top: 15px;
    padding-bottom: 10px;
    border-bottom: 0.5px solid #e9e9e9;
    &:last-child {
      border-bottom: none;
    }
  }
  &-item-active {
    color: @c-color-primary;
  }
}
</style>
